// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// s [author] rhyme_lph
// f [email]  rhymelph@gmail.com
// Q [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/src/entities/bill_detail_entity.dart';
import 'package:finance_app/src/entities/format/service_and_product_entity.dart';
import 'package:finance_app/src/http_service/http_bill_service.dart';
import 'package:finance_app/src/http_service/http_invoice_service.dart';
import 'package:finance_app/src/mixin/search_data_list_mixin.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:flutter/material.dart';

class SelectItemProvider extends BaseProvider<BillDetailEntity>
    with SearchDataListMixin {
  @override
  Future get dataFuture => isCredit
      ? HttpInvoiceService.showInvoice(billInvoiceId)
      : HttpBillService.showBill(billInvoiceId);

  bool isCredit = false;
  int billInvoiceId;

  List<ServiceAndProductEntity> items = [];

  @override
  void searchRefreshData(searchData) {}

  void onSelectItem(ServiceAndProductEntity item) async {
    final result = await Routes.navigateTo(RouterGenProviders.itemAmountPage,
        params: {'item': item, 'isCredit': isCredit});
    obtainContext?.call((context) {
      Navigator.of(context)..pop(result);
    });
  }

  @override
  FutureOr<BillDetailEntity> transformData(BillDetailEntity d, bool isCache) {
    items = d.items.map((e) => e.toServiceAndProduct()).toList();
    return super.transformData(d, isCache);
  }
}
